package com.future.hedongjun.mine.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.utils.PatternUtils;
import com.future.baselib.utils.TxtUtils;
import com.future.hedongjun.R;
import com.future.hedongjun.common.Contant;
import com.future.hedongjun.common.MyApp;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditPhone1Activity extends BaseActivity {

    public static final String KEY_TYPE = "type";
    @BindView(R.id.btn_getCode)
    Button btnGetCode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    private String phone;
    private int type;
    private String info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_phone1);
        ButterKnife.bind(this);
        if (type == 1) {
            setTitle("修改绑定手机号");
        } else {
            setTitle("修改密码");
        }
        phone = MyApp.getInstance().getUserInfo().getMobile();
        tvPhone.setText("手机号:" + TxtUtils.hidePhoneMiddle(phone));
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        if (extras != null) {
            type = extras.getInt(KEY_TYPE, 1);
        }
    }

    private void getCode() {
        String event = "resetpwd";
//        if (type == 1) {
//            event = "changemobile";
//        } else {
//            event = "resetpwd";
//        }
        btnGetCode.setClickable(false);
        new HttpRequest<DefaultResponse>()
                .with(this)
                .setPath("/api/Sms/send")
                .addParam("mobile", phone)
                .addParam("event",event)
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        info = response.data;
                        new Thread(runnable).start();
                    }
                })
                .addFailListener(new HttpRequest.OnNetworkFail() {
                    @Override
                    public void onFail(String message) {
                        toast(message);
                        btnGetCode.setClickable(true);
                    }
                }).start(new DefaultResponse());
    }

    //验证码发送倒计时
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    btnGetCode.setClickable(false);
                }
            });
            for (int i = 60; i > 0; i--) {
                final int finalI = i;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        btnGetCode.setText("重新获取(" + finalI + ")");
                    }
                });
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    btnGetCode.setClickable(true);
                    btnGetCode.setText("重新获取");
                }
            });
        }
    };

    @OnClick({R.id.btn_getCode, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_getCode:
                getCode();
                break;
            case R.id.btn_ok:
                final String code = etCode.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    toast("请输入验证码");
                    return;
                }
                new HttpRequest<DefaultResponse>()
                        .with(this)
                        .setPath("/api/Sms/check")
                        .addParam("mobile",MyApp.getInstance().getUserInfo().getMobile())
                        .addParam("captcha",code)
                        .addParam("event","resetpwd")
                        .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                            @Override
                            public void onSuccess(BaseResponse response) {
                                if (type == 1) {
                                    startActivity(EditPhone2Activity.class);
                                } else {
                                    Bundle bundle = new Bundle();
                                    bundle.putString("code",code);
                                    startActivity(EditPwdActivity.class, bundle);
                                }
                            }
                        }).start(new DefaultResponse());

                break;
        }
    }
}
